// 注册所有会用到的过滤器
// @Author Heroic Yang

package filters

import (
	//"errors"
	//"dhfshop/models"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

func RegisterFilters() {

	// 客户端务必在请求头中带上 `Authorization= Bearer "authorizeToken"`
	beego.InsertFilter("/rest/*", beego.BeforeRouter, authenticationFilter)
	beego.InsertFilter("/ws/*", beego.BeforeRouter, authenticationFilter)
	beego.InsertFilter("/wsauto/*", beego.BeforeRouter, authenticationFilter)
	beego.InsertFilter("/public/*", beego.BeforeRouter, crossAdmin)

}

func crossAdmin(ctx *context.Context) {
	//管理接口
	ctx.Output.Header("Access-Control-Allow-Origin", "*")
	ctx.Output.Header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
	ctx.Output.Header("Access-Control-Allow-Headers", "*, X-Requested-With, X-Prototype-Version, X-CSRF-Token, Content-Type, Authorization, dhfbackstoreid")

	if ctx.Input.Method() == "OPTIONS" {
		ctx.Output.SetStatus(200)
		ctx.Output.JSON("{}", false, false)
	}
}
